/*
Copyright (c) 2009, Heinrich C. Spreiter
All rights reserved.

Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

  - Redistributions of source code must retain the above copyright notice,
  this list of conditions and the following disclaimer.
  
  - Redistributions in binary form must reproduce the above copyright notice,
  this list of conditions and the following disclaimer in the documentation
  and/or other materials provided with the distribution.
  
  - Neither the name of the author nor the names of its contributors may be used
  to endorse or promote products derived from this software without specific
  prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package ch.kynlang.compiler.symbol;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.util.Map;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JColorChooser;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JProgressBar;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JSlider;
import javax.swing.JSpinner;
import javax.swing.JSplitPane;
import javax.swing.JTabbedPane;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.JToggleButton;
import javax.swing.JToolBar;
import javax.swing.JTree;

import ch.kynlang.compiler.EvaluationException;
import ch.kynlang.compiler.KohYaoNoiLexer;
import ch.kynlang.compiler.tree.KynTree;
import ch.kynlang.compiler.type.DefaultType;

/*
 * ComponentSymbolFactory.java
 *
 * @version 0.1 06.01.2010
 * @author HSP spreiter301@gmail.com
 * 
 * Factory to create components with default values
 */
public class ComponentSymbolFactory {
	// constants for components
	public static final String BUTTON = "button";
	public static final String CHECKBOX = "checkbox";
	public static final String COMBOBOX = "combobox";
	public static final String COLORCHOOSER = "colorchooser";
	public static final String LABEL = "label";
	public static final String LIST = "list";
	public static final String PASSWORDFIELD = "passwordfield";
	public static final String RADIOBUTTON = "radiobutton";
	public static final String SLIDER = "slider";
	public static final String SPINNER = "spinner";
	public static final String TABLE = "table";
	public static final String TEXTAREA = "textarea";
	public static final String TEXTFIELD = "textfield";
	public static final String TOGGLEBUTTON = "togglebutton";
	public static final String TREE = "tree";
	public static final String PANEL = "panel";
	public static final String PROGRESSBAR = "progressbar";
	public static final String SEPARATOR = "separator";
	
	
	// containers
	public static final String GRIDBAG = "gridbag";
	public static final String FLOW = "flow";
	public static final String BORDER = "border";
	public static final String SCROLL = "scroll";
	public static final String TAB = "tab";
	public static final String SPLIT = "split";
	public static final String TOOLBAR = "toolbar";
	
	public static final String[] CONTAINERS = {GRIDBAG, FLOW, BORDER, SCROLL, TAB, SPLIT, TOOLBAR};
	
	private ComponentSymbolFactory() {}
	
	/**
	 * creates a new ComponentSymbol
	 * @param tree WIDGET KynTree
	 * @return a new Component or null if not successfull
	 */
	public static ComponentSymbol createComponent(KynTree tree) {
		String type = tree.getChild(0).getText();

		if(type.equals(BUTTON)) {
			return createUIComponent(tree, JButton.class);
		}
		if(type.equals(CHECKBOX)) {
			UIComponent uic = createUIComponent(tree, JCheckBox.class);
			putAllProperties(uic.parentProperties, 
					createProperty(tree, int.class, "java.awt.GridBagConstraints.WEST", "anchor"));
			return uic;
		}
		if(type.equals(COMBOBOX)) {
			return createUIComponent(tree, JComboBox.class);
		}
		if(type.equals(COLORCHOOSER)) {
			return createUIComponent(tree, JColorChooser.class);
		}
		if(type.equals(LABEL)) {
			UIComponent uic = createUIComponent(tree, JLabel.class);
			putAllProperties(uic.parentProperties, 
					createProperty(tree, int.class, "java.awt.GridBagConstraints.WEST", "anchor"));
			return uic;
		}
		if(type.equals(LIST)) {
			return createUIComponent(tree, JList.class);
		}
		if(type.equals(PASSWORDFIELD)) {
			UIComponent uic = createUIComponent(tree, JPasswordField.class);
			putAllProperties(uic.parentProperties, 
					createProperty(tree, int.class, "java.awt.GridBagConstraints.HORIZONTAL", "fill"));
			return uic;
		}
		if(type.equals(RADIOBUTTON)) {
			UIComponent uic = createUIComponent(tree, JRadioButton.class);
			putAllProperties(uic.parentProperties, 
					createProperty(tree, int.class, "java.awt.GridBagConstraints.WEST", "anchor"));
			return uic;
		}
		if(type.equals(SLIDER)) {
			return createUIComponent(tree, JSlider.class);
		}
		if(type.equals(SPINNER)) {
			return createUIComponent(tree, JSpinner.class);
		}
		if(type.equals(TABLE)) {
			return createUIComponent(tree, JTable.class);
		}
		if(type.equals(TEXTAREA)) {
			return createUIComponent(tree, JTextArea.class);
		}
		if(type.equals(TEXTFIELD)) {
			UIComponent uic = createUIComponent(tree, JTextField.class);
			putAllProperties(uic.parentProperties, 
					createProperty(tree, int.class, "java.awt.GridBagConstraints.HORIZONTAL", "fill"));
			return uic;
		}
		if(type.equals(TOGGLEBUTTON)) {
			return createUIComponent(tree, JToggleButton.class);
		}
		if(type.equals(TREE)) {
			return createUIComponent(tree, JTree.class);
		}
		if(type.equals(PROGRESSBAR)) {
			return createUIComponent(tree, JProgressBar.class);
		}
		if(type.equals(PANEL)) {
			return createUIComponent(tree, JPanel.class);
		}
		if(type.equals(SEPARATOR)) {
			return createUIComponent(tree, JToolBar.Separator.class);
		}
		if(type.equals(GRIDBAG)) {
			ContainerSymbol cs = new ContainerSymbol(tree);
			cs.type = new DefaultType(JPanel.class, KohYaoNoiLexer.WIDGET);
			cs.canBeRoot = true;
			cs.maxChildCount = ContainerSymbol.INFINIT_CHILDREN;
			cs.container = GridBagLayout.class;
			
			// set default gridbag constraints that will be applied to children
			putAllProperties(cs.containerProperties,
					createProperty(tree, int.class, "java.awt.GridBagConstraints.RELATIVE", "gridx"),
					createProperty(tree, int.class, "java.awt.GridBagConstraints.RELATIVE", "gridy"),
					createProperty(tree, int.class, "1", "gridwidth"),
					createProperty(tree, int.class, "1", "gridheight"),
					createProperty(tree, double.class, "0.0", "weightx"),
					createProperty(tree, double.class, "0.0", "weighty"),
					createProperty(tree, int.class, "java.awt.GridBagConstraints.CENTER", "anchor"),
					createProperty(tree, int.class, "java.awt.GridBagConstraints.NONE", "fill"),
					createProperty(tree, Insets.class, "new java.awt.Insets(0,0,0,0)", "insets"),
					createProperty(tree, int.class, "0", "ipadx"),
					createProperty(tree, int.class, "0", "ipady"));
			return cs;
			
		}
		if(type.equals(TAB)) {
			ContainerSymbol cs = new ContainerSymbol(tree);
			cs.type = new DefaultType(JTabbedPane.class, KohYaoNoiLexer.WIDGET);
			cs.canBeRoot = false;
			cs.maxChildCount = ContainerSymbol.INFINIT_CHILDREN;
			cs.container = JTabbedPane.class;
			
			putAllProperties(cs.containerProperties,
					createProperty(tree, String.class, "\"\"", "tabtitle"));
			
			return cs;
		}
		if(type.equals(FLOW)) {
			ContainerSymbol cs = new ContainerSymbol(tree);
			cs.type = new DefaultType(JPanel.class, KohYaoNoiLexer.WIDGET);
			cs.canBeRoot = true;
			cs.maxChildCount = ContainerSymbol.INFINIT_CHILDREN;
			cs.container = FlowLayout.class;
			putAllProperties(cs.parentProperties, 
					createProperty(tree, int.class, "java.awt.GridBagConstraints.HORIZONTAL", "fill"));
			
			return cs;
		}
		if(type.equals(BORDER)) {
			ContainerSymbol cs = new ContainerSymbol(tree);
			cs.type = new DefaultType(JPanel.class, KohYaoNoiLexer.WIDGET);
			cs.canBeRoot = true;
			cs.maxChildCount = 5;
			cs.container = BorderLayout.class;
			
			// put components by default into CENTER
			putAllProperties(cs.containerProperties,
					createProperty(tree, String.class, "java.awt.BorderLayout.CENTER", "region"));
			return cs;
		}
		if(type.equals(SCROLL)) {
			ContainerSymbol cs = new ContainerSymbol(tree);
			cs.type = new DefaultType(JScrollPane.class, KohYaoNoiLexer.WIDGET);
			cs.canBeRoot = false;
			cs.maxChildCount = 1;
			cs.container = JScrollPane.class;
			
			return cs;
		}
		if(type.equals(SPLIT)) {
			ContainerSymbol cs = new ContainerSymbol(tree);
			cs.type = new DefaultType(JSplitPane.class, KohYaoNoiLexer.WIDGET);
			cs.canBeRoot = false;
			cs.maxChildCount = 2;
			cs.container = JSplitPane.class;
			
			return cs;
		}
		if(type.equals(TOOLBAR)) {
			ContainerSymbol cs = new ToolBar(tree);
			cs.type = new DefaultType(JToolBar.class, KohYaoNoiLexer.WIDGET);
			cs.canBeRoot = false;
			cs.maxChildCount = -1;
			cs.container = JToolBar.class;
			
			return cs;
		}
		
		// try to use custom component
		Symbol custom = tree.scope.resolve(type);
		if(custom != null && custom instanceof ImportSymbol) {
			ImportSymbol is = (ImportSymbol) custom;
			try {
				is.evaluate(); // symbol gets evaluated a seccond time later
				if(JComponent.class.isAssignableFrom(is.type.getJavaClass())) {
					return createUIComponent(tree, is.type.getJavaClass());
				}
			} catch(EvaluationException e) {
				// doesn't matter
			}
		}
		
		return null;
	}

	/**
	 * helper method to create a new UI Component with no default values
	 * @param tree
	 * @param clazz
	 * @return
	 */
	private static UIComponent createUIComponent(KynTree tree, Class<?> clazz) {
		UIComponent component = new UIComponent(tree);
		component.type = new DefaultType(clazz, KohYaoNoiLexer.WIDGET);
		return component;
	}
		
	/**
	 * helper method to create a new PropertySymbol
	 * @param tree
	 * @param clazz
	 * @param value
	 * @param name
	 * @return the propertysymbol
	 */
	private static PropertySymbol createProperty(KynTree tree, Class<?> clazz, String value, String name) {
		PropertySymbol ps = new PropertySymbol(tree);
		ps.type = new DefaultType(clazz, KohYaoNoiLexer.PROPERTY);
		ps.propertyValue = value;
		ps.name = name;
	
		return ps;
	}
	
	/**
	 * helper method to put a list of propertysymbols into the map
	 * @param map
	 * @param propertySymbols
	 */
	private static void putAllProperties(Map<String, PropertySymbol> map, PropertySymbol ... propertySymbols) {
		for(PropertySymbol ps : propertySymbols) {
			map.put(ps.name, ps);
		}
	}
}
